package com.ht.web.student;

import com.ht.bean.education.Classtype;
import com.ht.bean.emp.Empinfo;
import com.ht.bean.emp.Post;
import com.ht.bean.json.JsonData;
import com.ht.bean.student.*;
import com.ht.service.education.ClasstypeService;
import com.ht.service.emp.EmpinfoService;
import com.ht.service.student.*;
import com.ht.util.Contants;
import com.ht.util.Pager;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 王钟华
 * @date 2020-06-10 10:53
 */
@Controller
@RequestMapping("studentClass")
public class StudentClassController {

    @Resource
    private JsonData jsonData;
    @Resource
    private StudentclassService studentclassService;//班级管理接口
    @Resource
    private EmpinfoService empinfoService;//员工管理接口
    @Resource
    private DeptService deptService;//系管理接口
    @Resource
    private MajorService majorService;//专业管理接口
    @Resource
    private StudentfallService studentfallService;//届别管理接口
    @Resource
    private StudentService studentService;//学生管理接口
    @Resource
    private ClasstypeService classtypeService;//班级类别接口


    @RequestMapping("data")
    @ResponseBody
    public JsonData data(@Param("limit")int limit , @Param("pager")int page, @Param("studentclass")Studentclass studentclass){
        Pager pager = new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(studentclassService.selCount(studentclass));
        jsonData.setData(studentclassService.sybase(pager,studentclass));
        return jsonData;
    }


    /**
     * @return
     * 去班级添加
     */
    @RequestMapping("/toadd")
    public String toadd(Map map) {
        List<Empinfo> empinfoList = empinfoService.list();//员工编号
        List<Dept> deptList = deptService.allDept();//系管理
        List<Studentfall> studentfallList = studentfallService.selList();//届别管理
        List<Major> majorList = majorService.selList();//专业表
        List<Classtype> classtypeList=classtypeService.alldata();//班级类别
        List<Empinfo> classTeacherList = empinfoService.selByPostId(Contants.POST_CLASS_TEACHER);//所有班主任
        List<Empinfo> subjectTeacherList = empinfoService.selByPostId(Contants.POST_SUBJECT_TEACHER);//所有授课老师
        List<Empinfo> subjectTeacherList2 = empinfoService.selByPostId(Contants.POST_ASSISTANT);//所有授课老师
        for (Empinfo e:subjectTeacherList2) {
            subjectTeacherList.add(e);
        }
        map.put("classTeacherList",classTeacherList);
        map.put("subjectTeacherList",subjectTeacherList);
        map.put("empinfoList",empinfoList);
        map.put("deptList",deptList);
        map.put("studentfallList",studentfallList);
        map.put("majorList",majorList);
        map.put("classtypeList",classtypeList);
        return "student/studentClassAdd";
    }

    /**
     * @return
     * 去班级修改
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Map map,Integer id) {
        Studentclass studentclass = studentclassService.selectByPrimaryKey(id);
        List<Empinfo> empinfoList = empinfoService.list();//员工编号
        List<Empinfo> classTeacherList = empinfoService.selByPostId(Contants.POST_CLASS_TEACHER);//所有班主任
        List<Empinfo> subjectTeacherList = empinfoService.selByPostId(Contants.POST_SUBJECT_TEACHER);//所有授课老师
        List<Empinfo> subjectTeacherList2 = empinfoService.selByPostId(Contants.POST_ASSISTANT);//所有授课老师
        for (Empinfo e:subjectTeacherList2) {
            subjectTeacherList.add(e);
        }
        List<Classtype> classtypeList=classtypeService.alldata();//班级类别
        List<Major> majorList = majorService.selList();//专业表
        List<Dept> deptList = deptService.allDept();//系管理
        List<Studentfall> studentfallList = studentfallService.selList();//届别管理
        map.put("majorList",majorList);
        map.put("empinfoList",empinfoList);
        map.put("classTeacherList",classTeacherList);
        map.put("subjectTeacherList",subjectTeacherList);
        map.put("studentClass",studentclass);
        map.put("classtypeList",classtypeList);
        map.put("deptList",deptList);
        map.put("studentfallList",studentfallList);
        return "student/studentClassUpdate";
    }

    /**
     * @param studentclass
     * @return
     * 添加班级
     */
    @RequestMapping("add")
    @ResponseBody
    public Integer add(Studentclass studentclass){
        List<Studentclass> list = studentclassService.selList();
        for (Studentclass studentclass1:list) {
            if (studentclass.getClassname().equals(studentclass1.getClassname()) || studentclass1.getClassname()==studentclass.getClassname()
            ){
                return 0;
            }
        }
        studentclass.setCount(0);
        Integer i = studentclassService.insertSelective(studentclass);
        return i;
    }

    /**
     * @return
     * 去班级页面
     */
    @RequestMapping("list")
    public String list(Map map){
        List<Dept> dept= deptService.allDept();
        List<Studentclass> studentclasse= studentclassService.selList();
        map.put("dept",dept);
        map.put("studentclasse",studentclasse);
        return "student/studentClass";
    }



    /**
     * @return
     * 删除班级
     */
    @RequestMapping("del")
    @ResponseBody
    public String del(@Param("id") Integer id){
        System.out.println("id = " + id);
        int c= studentService.selbyclassid(id);
        System.out.println(c);
        if(c==0){
            studentclassService.deleteByPrimaryKey(id);
            return "true";
        }
        return "false";
    }


    /**
     * @param id
     * @return 根据系id查找所在该系下的专业
     */
    @RequestMapping("byDeptId")
    @ResponseBody
    public List byDeptId(Integer id) {
        List<Major> l  = majorService.byDeptId(id);
        return l;
    }

    /**
     * @param studentclass
     * @return 修改班级
     */
    @RequestMapping("update")
    @ResponseBody
    public Integer update(Studentclass studentclass) {
        List<Studentclass> list = studentclassService.selList();
        for (Studentclass studentclass1 : list) {
            if (studentclass.getClassid() != null){
                if (studentclass.getClassid() == studentclass1.getClassid()){
                    continue;
                }
            }
            if (studentclass.getClassname().equals(studentclass1.getClassname()) || studentclass1.getClassname() == studentclass.getClassname()) {
                return 0;
            }
        }
        Integer i = studentclassService.updateByPrimaryKeySelective(studentclass);
        return i;
    }
}
